<link href="/css/pagination.css" media="all" rel="stylesheet" type="text/css" />
<style type="text/css">
#main { text-align: center; }
#main table { width: 560px; border: 1px solid #F8F8FF; margin: 0 auto; background-color: #F5FFFA }
#main table tr { text-align: left; }
#main textarea { width: 200px; height: 86px;  }
#main table span { color: red; padding: 2px; font-weight: bold; }
#main #outline { color: blue; }
#main u { color: red; font-weight: bold; }


#main h4 { color:#000; background-color: transparent; width: 560px; padding: 3px; font-size: 15px; }
#main .list { text-align:left; padding-left: 30px; padding-bottom:20px; }
#main .list textarea { width: 560px; height: 60px; }
#main .list .btn { background: #0066CC; color: white; width: 80px; height: 28px; }
#main .list .btn_mouseover { background: #006699; padding:0px; }
#main .list .btn_mousedown { background: #0066FF; padding:2px; }
#main .list .item { color: #555; width: 68%; border: 1px dotted #ccc; padding: 2px; margin-bottom: 15px; }
#main .list .user { padding: 5px; background-color: #fff; }
#main .list .reply { background-color: rgb(255,255,232); padding: 5px; margin-top: 5px; }
#main .time { color: #999; font-style: italic }
#main .item .email { color: #ff0000; margin-left: 20px; }
</style>
<div id="main">
<h1>反馈管理</h1>

<div class="list">
<h4>共有 <span style="color:red; font-weight: bold;"><%=@feedlist.total_entries %></span> 条建议：</h4>
<div>全选：<input type="checkbox" id="cbxall" /> &nbsp;
	反选：<input type="checkbox" id="cbxun" /> &nbsp;
	<input type="button" value="删除选中项" id="btnDeleteSelected" />
</div>
<% @feedlist.each do |feed| %>
<div class="item">
    <div class="user"><input type="checkbox" name="cbxid" value="<%=feed.id%>" />
        <span style="color:rgb(86,137,0)"><%=feed.name%>：</span>
        <span class="time"><%=feed.created_at.in_time_zone(+8.hours).strftime("%Y-%m-%d %H:%M:%S")%></span>
        <span class="email"><%=feed.email%></span>
    <br/><%=raw(feed.content.gsub(/\r\n/, '<br/>'))%>
    </div>
    <div class="reply"><span style="color:rgb(204,51,0)">管理员：</span>
        <span class="time"><%=feed.updated_at.in_time_zone(+8.hours).strftime("%Y-%m-%d %H:%M:%S")%></span>
        <br/><textarea id="reply<%=feed.id%>"><%=feed.reply%></textarea>
        <input type="button" value="保存" onclick="__reply(<%=feed.id%>);" /> &nbsp;
        <input type="button" value="删除" onclick="return __delete(<%=feed.id%>)" />
    </div>
</div>
<% end %>

<div class='flickr_pagination' style="text-align:left;"><%= will_paginate @feedlist, :previous_label => '上页', :next_label => '下页' %></div>
</div>
</div>

<script type="text/javascript">

// 删除回复信息
function __delete(id) {
	if(!confirm('确定要删除吗?')) return false;
	if(id > 0) {
		$.ajax({
			type : 'POST',
			url : '/admin/feedback/delete',
			data : { 'id' : id },
			success: function(d){
				if(d == 1) {
					alert('删除成功。');
					$('#reply' + id).parents('div.item').remove();
				} else {
					alert('删除失败，请重新尝试。');
				}
			}
		});
	}
	return true;
}

// 保存回复信息
function __reply(id) {
	if(id > 0) {
		$.ajax({
			type : 'POST',
			url : '/admin/feedback/reply',
			data : { 'id' : id, 'reply' : $('#reply' + id).val() },
			success: function(d){
				if(d == 1) {
					alert('保存成功。');
				} else {
					alert('保存失败，请重新尝试。');
				}
			}
		});
	}
}

// 获取所有选中项的id号
function getSelectedIDs() {
	var arrID = [];
	$(':checkbox[name="cbxid"]:checked').each(function(){
		arrID.push(this.value);
	});
	return (arrID.join(','));
}

$().ready(function(){


	$.ajaxSetup({  
	    beforeSend: function(xhr) {  
	        xhr.setRequestHeader("X-CSRF-Token", $("meta[name=\"csrf-token\"]").attr("content"));  
	    }  
	});		

	// 全选
	$('#cbxall').click(function(){
		var _checked = this.checked;
		$(':checkbox[name="cbxid"]').attr('checked', _checked);
	});

	// 反选
	$('#cbxun').click(function(){
		$(':checkbox[name="cbxid"]').each(function(){
			this.checked = !this.checked;
		});
	});

	// 删除选中项
	$('#btnDeleteSelected').click(function(){
		if(!confirm('确认要删除选中项？')) return;
		var ids = getSelectedIDs();
		if(ids) {
			$.ajax({
				type : 'POST',
				url : '/admin/feedback/deletes',
				data : { 'ids' : ids },
				success: function(d){
					if(d > 0) {
						alert('已成功删除 '+ d +' 条信息。');
						location.href = location.href;
					} else {
						alert('删除失败，请重新尝试。');
					}
				}
			});
		}
	});

});
</script>
